package com.Servlet;

import com.Service.AdminService;
import com.Service.Impl.AdminServiceImpl;
import com.entity.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author: 冷俊杰
 * @Description: 接受邮箱验证判断
 * @param验证码的比对
 * @returnjsp页面的跳转
 * @DateTime: 2022/5/29 15:28
 **/
@WebServlet("/RegistServlet")
public class RegistServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String sessionCode = (String) request.getSession().getAttribute("code");
        //  获取session中的验证码
        if (sessionCode != null) {
            //  获取页面提交的验证码
            String inputCode = request.getParameter("code");
            System.out.println("页面提交的验证码:" + inputCode);
            System.out.println(sessionCode.toLowerCase().equals(inputCode.toLowerCase()));
            if (sessionCode.toLowerCase().equals(inputCode.toLowerCase())) {
                String email = request.getParameter("email");
                String password = request.getParameter("password");
                AdminService service = new AdminServiceImpl();
                Admin admin = service.AdminByNP(email, password);
                if (admin != null) {
                    request.getRequestDispatcher("./SelectUserByHs").forward(request, response);
                } else {
                    request.getRequestDispatcher("./remain.jsp").forward(request, response);
                }
            } else {
                //  验证失败
                request.getRequestDispatcher("./remain.jsp").forward(request, response);
            }
        } else {
            //  验证失败
            request.getRequestDispatcher("./remain.jsp").forward(request, response);
        }
        //  移除session中的验证码
        request.removeAttribute("code");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
